﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace AppDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            label1.SetBinding(Label.ContentProperty, new Binding("MyProperty") { Source = this,NotifyOnSourceUpdated=true });
        }

        public int MyProperty { get; set; }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //MyProperty = 2;
            
        }

        private void demo1_TextChanged(object sender, TextChangedEventArgs e)
        {
            MyProperty = 2;
        }
    }

    public class DependencyNotifyBehavior : Behavior<FrameworkElement>
    {

        protected override void OnAttached()
        {

            base.OnAttached();
        }

        public object DependencyObject
        {
            get { return (object)GetValue(DependencyObjectProperty); }
            set { SetValue(DependencyObjectProperty, value); }
        }

        public static readonly DependencyProperty DependencyObjectProperty =
            DependencyProperty.Register("DependencyObject", typeof(object), typeof(DependencyNotifyBehavior),
            new UIPropertyMetadata((o, a) =>
            {
                var behavior = o as DependencyNotifyBehavior;
                if (behavior.AssociatedObject == null) return;
                BindingOperations.GetBindingExpression(behavior.AssociatedObject as Label, behavior.NotifyObject).UpdateTarget();
            }));


        public DependencyProperty NotifyObject { get; set; }
    }
}
